/** 城墙、低价培训基地、中阶培训基地、高阶培训基地 */
var updatearea = require("./updatearea");
cc.Class({
    extends: cc.Component,

    properties: {
        /** 升级对象 */
        upObj:updatearea,
        /** 标题文本 */
        txt_name:cc.Label,
        /** 行对象 */
        pf_line:cc.Prefab,
        /** 行节点 */
        nd_line:cc.Node,
        /** 加速按钮 */
        bt_study:cc.Button,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.bt_study.node.on(cc.Node.EventType.TOUCH_END, function(ev){
            cc.log("bt_study");
        }, this);
    },

    /** 设置位置id */
    setAid(aid){
        let bd = App.U.city[App.cid].area[aid-1], fid = [], race = App.U.city[App.cid].race, ptsf = null;
        if(bd.bid == 9){
            this.txt_name.string = "炼金实验室";
            fid = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
        }else if(bd.bid == 10){
            this.txt_name.string = "城墙";
            fid = [30,30,30];
            ptsf = new cc.SpriteFrame(App.path + "alchemy/" + race + App.C.alchemy[0][30].img + ".png");
        }else if(bd.bid == 22){
            this.txt_name.string = "低价培训基地";
            fid = [20,21,22];
        }else if(bd.bid == 23){
            this.txt_name.string = "中阶培训基地";
            fid = [23,24,25];
        }else if(bd.bid == 24){
            this.txt_name.string = "高阶培训基地";
            fid = [26];
        }
        this.nd_line.removeAllChildren();
        for(let i=0;i<fid.length;i++){
            let obj = cc.instantiate(this.pf_line), demo = "", lev = 0, val = "", val2 = "";
            if(bd.bid == 9 || bd.bid == 10){
                if(bd.bid == 10){
                    obj.getChildByName("sp_pic").getComponent(cc.Sprite).spriteFrame = ptsf;
                    if(bd.fids[i]) lev = bd.fids[i];
                }else{
                    obj.getChildByName("sp_pic").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(App.path + "alchemy/" + App.C.alchemy[0][fid[i]].img + ".png");
                    if(bd.fids[fid[i]]) lev = bd.fids[fid[i]];
                }
                demo = App.C.alchemy[0][fid[i]].desc;
                val = App.C.alchemy[0][fid[i]].list[lev][9];
                obj.getChildByName("txt_name").getComponent(cc.Label).string = App.C.alchemy[0][fid[i]].name;
            }else{
                if(bd.fids[fid[i]]) lev = bd.fids[fid[i]];
                val = App.C.alchemy[0][fid[i]][lev][9]|0;
                val2 = val / App.C.troop[14*(race-1)+(fid[i]-20)*2+2].strength;
                val = val / App.C.troop[14*(race-1)+(fid[i]-20)*2+1].strength;
                demo = App.C.alchemy[race][fid[i]].desc;
                obj.getChildByName("sp_pic").getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(App.path + "alchemy/" + App.C.alchemy[race][fid[i]].img + ".png");
                obj.getChildByName("txt_name").getComponent(cc.Label).string = App.C.alchemy[race][fid[i]].name;
            }
            obj.getChildByName("txt_lev").getComponent(cc.Label).string = lev + "级";
            let btup = obj.getChildByName("bt_up");
            if(bd.fid > 0){
                btup.getComponent(cc.Button).interactable = false;
                let olev = 0;
                if(bd.bid == 10 && bd.sid == i){
                    if(bd.fids[bd.sid]) olev = bd.fids[bd.sid];
                    obj.getChildByName("txt_lev").getComponent(cc.Label).string = lev + "->" + (olev + 1) + "级";
                }else if(bd.bid != 10 && bd.fid == fid[i]){
                    if(bd.fids[bd.fid]) olev = bd.fids[bd.fid];
                    obj.getChildByName("txt_lev").getComponent(cc.Label).string = lev + "->" + (olev + 1) + "级";
                }
            }else{
                btup.fid = fid[i];
                btup.sid = i;
                btup.on(cc.Node.EventType.TOUCH_END, this.upClick, this)
            }
            if(lev < 1) lev += 1;
            obj.getChildByName("txt_demo").getComponent(cc.Label).string = demo.replace('{0}', lev + "级").replace('{1}', val).replace('{2}', val2);
            obj.parent = this.nd_line;
        }
        if(bd.fid > 0){
            this.bt_study.node.active = true;
            this.bt_study.node.getChildByName("Label").getComponent(cc.Label).string = ((bd.endtime - App.dtm - Date.now())/1000|0) + " 加速";
        }else{
            this.bt_study.node.active = false;
        }
        this.nd_line.height = 122 * fid.length;
        this.upObj.showInfo(aid);
    },

    /** 升级按钮 */
    upClick(ev){
        cc.log("upclick", ev.target.fid, ev.target.sid);
        App.net.send([2,45,{cid:App.cid,aid:this.upObj._aid,fid:ev.target.fid,sid:ev.target.sid}]);
    },

    start () {

    },
    /** 接受到网络消息 */
    onNetData(msg){
        cc.log("traroom", msg[1]);
        if(msg[1] == 145){ //升级研究返回
            this.setAid(this.upObj._aid);
        }else if(msg[1] == 146){ //完成研究返回
            this.setAid(this.upObj._aid);
        }
    },
    // update (dt) {},
});
